﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Research.Kinect.Nui;
using System.Reactive.Subjects;
using System.Windows.Media.Media3D;

namespace Kinect.KinectMouseControl
{
    public class SkeletonTracker : IEventPublisher<SkeletonLocations>
    {
        private IEventPublisher<SkeletonFrame> _skeletonPublisher;

        private Subject<SkeletonLocations> _skeletonTracker = new Subject<SkeletonLocations>();

        public IObservable<SkeletonLocations> EventObservable { get { return _skeletonTracker; } }

        public SkeletonTracker(IEventPublisher<SkeletonFrame> skeletonPublisher)
        {
            _skeletonPublisher = skeletonPublisher;
            _skeletonPublisher.EventObservable.Subscribe(x => PublishDistance(x));
        }

        private void PublishDistance(SkeletonFrame skeletonFrame)
        {
            List<Point3D> trackedLocations = skeletonFrame
                            .Skeletons
                            .Where(x => x.TrackingState == SkeletonTrackingState.Tracked)
                            .Select(x => x.Joints[JointID.HipCenter].Position.ToPoint3D())
                            .ToList();

            _skeletonTracker.OnNext(
                new SkeletonLocations
                {
                    Points = trackedLocations 
                });
        }
    }
}
